package com.eco.fanliapp.controller.exapi;


import com.alibaba.fastjson.JSON;
import com.eco.fanliapp.common.response.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 商品（WechatSet）UserTaobaoDetailController
 */
@RestController
@RequestMapping("/web/user/wechat")
public class UserTaobaoDetailController {

//    private static final Logger log = LoggerFactory.getLogger(UserTaobaoDetailController.class);

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    // key过期时间（秒）
    private final Long EXPIRETIME = 60L;

    // 获取微信号
    @GetMapping("/getWechat")
    public ServerResponse<String> getWechat() {

        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");

        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }
        WechatSet wechatSet_ = new WechatSet();
        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);

        if (wechatSets != null && wechatSets.size() > 0) {
            for (WechatSet wechatSet : wechatSets) {
                int hasClickNum = wechatSet.getHasClickNum();
                int wechatClickNum = wechatSet.getWechatClickNum();
                if (wechatSet.getWechatCode() != null && hasClickNum < wechatClickNum) {
//                    wechatSet.setHasClickNum(hasClickNum + 1);
//                    stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(wechatSets));
                    wechatSet_ = wechatSet;
                    break;
                }

            }
        }
        return ServerResponse.createBySuccess(wechatSet_.getWechatCode()==null?"":wechatSet_.getWechatCode());
    }

    // 获取微信号
    @GetMapping("/clickWechat")
    public ServerResponse<String> clickWechat(String wechatCode) {

        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");

        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }
        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);

        if (wechatSets != null && wechatSets.size() > 0) {
            for (WechatSet wechatSet : wechatSets) {
                if (wechatSet.getWechatCode() != null && wechatSet.getWechatCode().equals(wechatCode)) {
                    int hasClickNum = wechatSet.getHasClickNum();
                    wechatSet.setHasClickNum(hasClickNum + 1);
                    stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(wechatSets));
                    break;
                }


            }
        }
        return ServerResponse.createBySuccess("成功");
    }
    // 获取所有微信号
    @GetMapping("/getWechatList")
    public ServerResponse<List<WechatSet>> getWechatList() {

        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");

        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }
        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);
        return ServerResponse.createBySuccess(wechatSets);
    }

    // 更新微信号
    @GetMapping("/updateWechat")
    public ServerResponse<String> updateWechat(WechatSet wechatSet) {
        if (wechatSet.getWechatCode() == null || "".equals(wechatSet.getWechatCode()) || wechatSet.getId() == 0) {
            return ServerResponse.createBySuccess("失败");
        }
        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");

        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }
        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);

        if (wechatSets != null && wechatSets.size() > 0) {
            for (WechatSet wechatSetNew : wechatSets) {
                if (wechatSetNew.getId() == wechatSet.getId()) {
                    wechatSetNew.setWechatClickNum(wechatSet.getWechatClickNum());
                    wechatSetNew.setWechatCode(wechatSet.getWechatCode());
                    wechatSetNew.setWechatOrder(wechatSet.getWechatOrder());
                    stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(wechatSets));
                    break;
                }
            }
        }

        return ServerResponse.createBySuccess("成功");
    }

    // 删除微信号
    @GetMapping("/deleteWechat")
    public ServerResponse<String> deleteWechat(int id) {
        if (id == 0) {
            return ServerResponse.createBySuccess("失败");
        }
        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");

        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }
        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);

        List<WechatSet> wechatSetsNew = new ArrayList<>();
        if (wechatSets != null && wechatSets.size() > 0) {
            for (WechatSet wechatSetNew : wechatSets) {
                if (wechatSetNew.getId() == id) {
                } else {
                    wechatSetsNew.add(wechatSetNew);
                }
            }
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(wechatSetsNew));
        }

        return ServerResponse.createBySuccess("成功");
    }

    // 添加微信号
    @GetMapping("/addWechat")
    public ServerResponse<String> addWechat(WechatSet wechatSet) {

        String wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        if (wechatList == null || "".equals(wechatList)) {
            stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(new ArrayList<WechatSet>()));
            wechatList = stringRedisTemplate.opsForValue().get("wechatListForPageH5");
        }

        List<WechatSet> wechatSets = JSON.parseArray(wechatList,WechatSet.class);

        int max = 1;
        if (wechatSets != null && wechatSets.size() > 0) {
            for (WechatSet wechatSetNew : wechatSets) {
                if (wechatSetNew.getId() > max) {
                    max = wechatSetNew.getId();
                }
            }
        }
        if (wechatSet.getWechatCode() == null || "".equals(wechatSet.getWechatCode())) {
            return ServerResponse.createBySuccess("失败");
        }
        wechatSet.setId(max + 1);
        wechatSet.setDeleted(0);
        wechatSet.setHasClickNum(0);
        wechatSet.setWechatOrder(0);
        wechatSets.add(wechatSet);
        stringRedisTemplate.opsForValue().set("wechatListForPageH5", JSON.toJSONString(wechatSets));

        return ServerResponse.createBySuccess("成功");
    }


}
